package com.heima.admin.controller.v1;

import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.admin.service.ChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理Controller",tags = "频道管理Controller",description = "频道管理Controller")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }
    @ApiOperation("条件分页查询频道列表")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody ChannelDTO dto){
        return channelService.findListByQuery(dto);
    }
    @ApiOperation(value = "保存频道信息",notes = "保存时会校验：频道名称不为空 频道名称不能超过10个字符，名称不能重复")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel adChannel){
        return channelService.insert(adChannel);
    }
    @ApiOperation(value = "修改频道信息",notes = "修改时会校验：如果频道名称有改动会检测频道名称不为空 频道名称不能超过10个字符，名称不能重复")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel){
        return channelService.update(adChannel);
    }
    @ApiOperation(value = "删除频道信息",notes = "如果频道有效，不能删除")
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return channelService.delete(id);
    }
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }

}
